package controller

import (
	"net/http"
	"strconv"

	"blog/internal/service"

	"github.com/gin-gonic/gin"
)

type ArticleController struct {
	service *service.ArticleService
}

func NewArticleController(service *service.ArticleService) *ArticleController {
	return &ArticleController{service: service}
}

func (c *ArticleController) GetAll(ctx *gin.Context) {
	articles, err := c.service.GetAll()
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	ctx.JSON(http.StatusOK, articles)
}

func (c *ArticleController) GetByID(ctx *gin.Context) {
	id := ctx.Param("id")
	// 转换id为uint
	idInt, err := strconv.Atoi(id)
	if err != nil {
		return
	}
	article, err := c.service.GetByID(uint(idInt))
	if err != nil {
		ctx.JSON(http.StatusNotFound, gin.H{"error": "article not found"})
		return
	}
	ctx.JSON(http.StatusOK, article)
}

// 其他CRUD方法...
